package book.service.impl;

import java.sql.Connection;
import java.sql.SQLException;

import book.dao.UserDao;
import book.dao.impl.UserDaoImpl;
import book.entity.User;
import book.service.UserService;
import book.utils.JdbcUtils;

public class UserServiceImpl implements UserService{

	UserDao userDao = new UserDaoImpl();
	@Override
	public User getUserById(Integer userId) throws Exception {
		
		Connection connection = JdbcUtils.getConnection();
		
		String sql = "SELECT user_id as userId,email,password FROM user WHERE user_id=?";
		User user = userDao.getUser(connection, sql,User.class,userId);
		
		return user;
	}
	
	
	@Override
	public User getUserByEP(String email, String password) {
		
		Connection connection = JdbcUtils.getConnection();
//		AND password=?
		String sql="SELECT user_id as userId,email,password FROM user WHERE email=? and password=?  ";
		
		try {
			connection.setAutoCommit(false);
		    User user = userDao.getUserByEP(connection, sql,User.class,email,password);
			connection.commit();
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			JdbcUtils.close();
			e.printStackTrace();
		}
		
		return null;
	}

}
